package com.nowcoder.community.service;

import com.nowcoder.community.dao.MessageMapper;
import com.nowcoder.community.entity.Message;
import com.nowcoder.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @author 韩
 * @version 1.0
 */
@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    public List<Message> findConversations(int userId, int offset, int limit) {
        return messageMapper.selectConversations(userId, offset, limit);
    }

    public int finConversationsCount(int userId) {
        return messageMapper.selectConversationsCount(userId);
    }

    public List<Message> findLetters(String conversationId, int offset, int limit) {
        return messageMapper.selectLetters(conversationId, offset, limit);
    }

    public int findLettersCount(String conversationId) {
        return messageMapper.selectLettersCount(conversationId);
    }

    public int findLetterUnreadCount(int userId, String conversationId) {
        return messageMapper.selectLettersUnReadCount(userId, conversationId);
    }

    public int addMessage(Message message) {
        //处理html标签和过滤敏感词
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        System.out.println("添加message");
        return messageMapper.insertMessage(message);
    }

    public int updateMessageStatus(List<Integer> ids) {
        return messageMapper.updateStatus(ids, 1);
    }

    public Message findLatestNotice(String topic, int userId) {
        return messageMapper.selectLatestNotice(topic, userId);
    }

    public int findNoticeCount(String topic, int userId) {
        return messageMapper.selectNoticeCount(topic, userId);
    }

    public int findNoticeUnreadCount(String topic, int userId) {
        return messageMapper.selectNoticeUnreadCount(topic, userId);
    }

    public List<Message> findNotices(String topic, int userId, int offset, int limit) {
        return messageMapper.selectNotices(topic, userId, offset, limit);
    }
}